package com.huangliusong.demo3;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanPostProcess implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println("【第五步：初始化前方法】"+o);
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(final  Object bean, String beanName) throws BeansException {
         System.out.println("【第八步：初始化后方法】"+bean);
        if ("userDao".equals(beanName)){
            Object proxy=  Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if ("save".equals(method.getName())){
                        //增强
                        System.out.println("权限校验");
                       return  method.invoke(bean,args);
                    }
                    return  method.invoke(bean,args);
                }
            });
            return  proxy;
        }else
        {
            System.out.println("userDao");
            return bean;
        }
    }
}
